package com.zjeav.lingjiao.ui.personCenter;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.dalimao.corelibrary.swipeback.SwipeBackActivity;
import com.dalimao.corelibrary.view.LoadProgressDialog;
import com.zjeav.lingjiao.R;
import com.zjeav.lingjiao.base.baseBean.Result;
import com.zjeav.lingjiao.base.request.ChanageMobileRequest;
import com.zjeav.lingjiao.base.request.ValidateCode;
import com.zjeav.lingjiao.base.response.ValidateCodeResponse;
import com.zjeav.lingjiao.base.utils.StringUtils;
import com.zjeav.lingjiao.base.utils.UIUtils;
import com.zjeav.lingjiao.ui.register.RegisterPresenter;
import com.zjeav.lingjiao.ui.register.RegisterView;

public class VerifyMobileActivity extends SwipeBackActivity implements RegisterView {

    EditText username_ed;
    EditText pw_ed;
    Button login_bn;
    TextView getsms;
    String type ;
    private LoadProgressDialog loadProgressDialog; //加载框
    RegisterPresenter presenter;
    String code;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.verify_mobile);

        login_bn = findViewById(R.id.login_bn);
        username_ed = findViewById(R.id.username_ed);
        pw_ed = findViewById(R.id.pw_ed);
        getsms = findViewById(R.id.getsms);

        presenter = new RegisterPresenter(this);
        loadProgressDialog = new LoadProgressDialog(this, com.dalimao.corelibrary.R.style.MyDialog);

        getsms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("".equals(username_ed.getText().toString())) {
                    Toast.makeText(VerifyMobileActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                } else {
                    loadProgressDialog.show();
                    timer.start();
                    presenter.getCode(new ValidateCode(username_ed.getText().toString(),type));
                }
            }
        });

        login_bn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (StringUtils.isNullOrEmpty(username_ed.getText().toString())){
                    Toast.makeText(VerifyMobileActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();
                }else if (StringUtils.isNullOrEmpty(pw_ed.getText().toString())){
                    Toast.makeText(VerifyMobileActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                }else {
                    loadProgressDialog.show();
                    ChanageMobileRequest mobileRequest = new ChanageMobileRequest(username_ed.getText().toString(),pw_ed.getText().toString());
                    presenter.chanagemobile(mobileRequest);
                }
            }
        });

    }

    @Override
    public void showCodeSuccess(ValidateCodeResponse response) {
        loadProgressDialog.dismiss();
        code = response.getData();
    }

    @Override
    public void showCodeFail() {
        loadProgressDialog.dismiss();
        Toast.makeText(VerifyMobileActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
    }

    /**
     * 更改手机号成功
     */
    @Override
    public void registerSuccess(Result result) {
        loadProgressDialog.dismiss();
        UIUtils.showToast("更改手机号成功");
        Intent intent = new Intent();
        intent.putExtra("editname",username_ed.getText().toString());
        setResult(5,intent);
        finish();
    }

    // 第一个参数表示总时间，第二个参数表示间隔时间。意思就是每隔一秒会回调一次方法onTick，然后60秒之后会回调onFinish方法
    final CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long arg0) {
            // 定期定期回调
            getsms.setClickable(false);
            getsms.setText((arg0 / 1000) + "秒后可重发");
        }

        @Override
        public void onFinish() {
            // 结束后回到
            getsms.setText("重新获取");
            getsms.setClickable(true);
            getsms.setBackground(getResources().getDrawable(R.drawable.buttonstyle));
        }
    };
}
